package org.yaac.shared.property;

import static com.google.common.collect.Lists.newArrayList;

import java.util.Arrays;
import java.util.List;

import org.yaac.client.util.AutoBeanUtil;
import org.yaac.shared.egql.ResultCell;
import org.yaac.shared.egql.ResultCell.ResultCellFactory;
import org.yaac.shared.util.StringUtils;

import com.google.gwt.safehtml.shared.SafeHtmlUtils;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class BlobKeyPropertyInfo extends PropertyInfo {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String blobKey;
	
	/**
	 * 
	 */
	@SuppressWarnings("unused")
	private BlobKeyPropertyInfo(){}
	
	/**
	 * @param blobKey
	 * @param downloadPath
	 */
	public BlobKeyPropertyInfo(String blobKey) {
		super();
		this.blobKey = blobKey;
	}

	public String getBlobKey() {
		return blobKey;
	}

	@Override
	public int compareTo(PropertyInfo o) {
		// blob key is not comparable
		return 0;
	}

	@Override
	public String asHtml() {
		// TODO : i18n
		String pathStr = AutoBeanUtil.blobDownladPathString(this.blobKey);
		return "<a href='/download?path=" + SafeHtmlUtils.htmlEscape(pathStr) + "' target='blank'>Blobstore Blob</a>";
	}

	@Override
	public String toString() {
		return "BlobKeyPropertyInfo [blobKey=" + blobKey + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((blobKey == null) ? 0 : blobKey.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BlobKeyPropertyInfo other = (BlobKeyPropertyInfo) obj;
		if (blobKey == null) {
			if (other.blobKey != null)
				return false;
		} else if (!blobKey.equals(other.blobKey))
			return false;
		return true;
	}

	@Override
	public ResultCell populateResultCell(ResultCellFactory factory) {
		ResultCell cell = super.populateResultCell(factory);
		cell.setType(PropertyType.BLOB_KEY);
		cell.setParent(null);
		cell.setChildren(null);
		cell.setPayload(Arrays.asList(blobKey));
		return cell;
	}

	@Override
	public String asExpression(FilePlaceHolder placeHolder) {
		return "blobkey(" + StringUtils.wrap(this.blobKey, "'") + ")";
	}
	
	@Override
	public List<String> downloadPaths() {
		return newArrayList();
	}
}
